Namespace Actions.ActionItems
    <ActionItemWordAttribute("filterfile")> _
Public Class aiFilterFile
        Inherits ActionItem
        Public LineCount As Integer = 0
        Public InFile As String = ""
        Public OutFile As String = ""
        Public RegExpPattern As String = ""
        Public HasHeader As Boolean = True

        Public Sub New(ByVal Owner As Action, ByVal Definition As Xml.XmlNode)
            MyBase.New(Owner, Definition)
        End Sub

        Public Overrides Sub Execute()
            InFile = OwnerAction.InsertParams(Configuration.Attribute("in"))
            OutFile = OwnerAction.Info.ResultsFolderName + "\" + OwnerAction.InsertParams(Configuration.Attribute("out"))
            RegExpPattern = OwnerAction.InsertParams(Configuration.Attribute("pattern", "."))

            LogMessage("Otwieram plik " + InFile)
            Dim Input As New IO.StreamReader(InFile)
            Dim Output As New IO.StreamWriter(OutFile)
            Dim InputLine As String
            Dim re As New System.Text.RegularExpressions.Regex(RegExpPattern)

            If HasHeader Then
                Output.WriteLine(Input.ReadLine)
            End If

            While Not Input.EndOfStream
                InputLine = Input.ReadLine
                If re.IsMatch(InputLine) Then
                    Output.WriteLine(InputLine)
                End If
            End While

        End Sub

    End Class
End Namespace


